package org.wisigoth.chat.client.jabber;

import org.wisigoth.chat.client.ChatSession;
import org.wisigoth.chat.client.Contact;
import org.wisigoth.chat.client.Message;
import org.wisigoth.chat.client.MessageConsumer;
import org.wisigoth.chat.client.MessageFilter;
import org.wisigoth.chat.client.Session;
import org.wisigoth.chat.client.msg.ChatMessage;

public class ChatSessionImpl implements ChatSession, MessageFilter {

	private Contact contact;
	private Session session;
	private MessageConsumer consumer;
	
	
	public ChatSessionImpl(Contact contact, Session session) {
		this.session = session;
		this.contact = contact;	
	}

	@Override
	public void consume(Message message) {
		this.consumer.consume(message);
	}
	
	
	@Override
	public Contact getContact() {
		return this.contact;
	}


	@Override
	public Session getSession() {
		return this.session;
	}

	@Override
	public void addMessageConsumer(MessageConsumer consumer) {
		this.consumer = consumer;
		
	}

	@Override
	public void removeMessageConsumer(MessageConsumer consumer) {
		this.consumer = null;
		
	}

	@Override
	public MessageFilter getFilter() {
		return this;
		
	}

	@Override
	public boolean accept(Message message) {
		return (message instanceof ChatMessage) && (message.getFrom().equals(this.contact.getUser()));
		
	}

	@Override
	public void sendMessage(ChatMessage message) {
		this.getSession().sendMessage(message);
	}

}
